<template>
    <el-container class="home-container">
        <!-- 头部区 -->
        <el-header>
            <div>
                <img src="../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主题区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <!-- 侧边栏菜单 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
                    :collapse="isCollapse" :collapse-transition='false' router :default-active="menuPath">
                    <!-- 折叠按钮 -->
                    <div class="toggle-button" @click="toggleCollapse">|||</div>
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="item in meaulist" :key="item.id">
                        <!-- 一级菜单模板 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconobj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+submenu.path" v-for="submenu in item.children"
                            @click="savePath('/'+submenu.path)">
                            <!-- 二级菜单模板 -->
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{submenu.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧主题区域 -->
            <el-main>
                <!-- 子组件路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        data() {
            return {
                meaulist: [],
                iconobj: {
                    '125': 'el-icon-s-custom',
                    '103': 'el-icon-s-check',
                    '101': 'el-icon-s-goods',
                    '102': 'el-icon-s-order',
                    '145': 'el-icon-s-marketing'
                },
                //是否折叠菜单栏的布尔值
                isCollapse: false,
                //选中连接的地址值
                menuPath: ''
            }
        },
        created() {
            this.getMenuList()
            this.menuPath = window.sessionStorage.getItem('activePath')
        },
        methods: {
            logout() {
                //清空token
                window.sessionStorage.clear()
                //路由重定向到登录页
                this.$router.push('/login')
            },
            async getMenuList() {
                const { data: res } = await this.$http.get('menus')
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.meaulist = res.data
            },
            toggleCollapse() {
                this.isCollapse = !this.isCollapse
            },
            savePath(savePath) {
                window.sessionStorage.setItem('activePath', savePath)
                this.menuPath = savePath
            }
        },
    }
</script>
<style lang="less" scoped>
    .home-container {
        height: 100%;
    }

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 20px;
    }

    .el-header div {
        display: flex;
        align-items: center;
    }

    .el-header div img {
        width: 56px;
        height: 61px;
    }

    .el-header div span {
        margin-left: 15px;
    }

    .el-aside {
        background-color: #333744;
    }

    .el-aside .el-menu {
        border-right: none;
    }

    .el-main {
        background-color: #EAEDF1;
    }

    .toggle-button {
        height: 30px;
        color: #fff;
        line-height: 24px;
        font-size: 10px;
        text-align: center;
        cursor: pointer;
        letter-spacing: 0.2em;
    }

    .toggle-button:hover {
        background-color: rgb(41, 44, 54);
    }
</style>